package com.web.main.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.Code;
import com.common.entity.ResponseResult;
import com.common.entity.Site;
import com.web.main.entity.form.SiteForm;
import com.web.main.entity.param.SiteQuery;
import com.web.main.entity.vo.SiteVo;
import com.web.main.service.SiteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 长顺
 * @since 2020-11-26
 */
@RestController
@Api(tags = "电站接口")
@RequestMapping("/site")
@Validated
@CrossOrigin
public class SiteController {
    @Autowired
    SiteService siteService;

    //测试接口
    @GetMapping("/getTestSite")
    public ResponseResult getTestSite() {
        List<Site> sites = new ArrayList<>();
        Site site1 = new Site();
        Site site2 = new Site();
        site1.setSiteAddress("广东");
        site1.setSitePlace(0);
        site1.setSiteName("傻逼");
        site2.setSiteAddress("广东");
        site2.setSitePlace(0);
        site2.setSiteName("傻逼");
        sites.add(site1);
        sites.add(site2);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data(sites);
    }


    @ApiOperation(value = "添加电站")
    @PostMapping("/add")
    public ResponseResult add(@ApiParam(value = "电站对象")
                              @Validated(SiteForm.Add.class) SiteForm siteForm) {
        siteService.addSite(siteForm);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "删除电站")
    @DeleteMapping("/delete")
    public ResponseResult deleteOne(@ApiParam(value = "电站主键id", required = true)
                                    @RequestParam("siteId") String siteId) {
        siteService.removeSiteById(siteId);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "修改电站")
    @PutMapping("/modify")
    public ResponseResult modify(@ApiParam(value = "电站对象", required = true)
                                 @Validated(SiteForm.Modify.class) SiteForm siteForm) {
        siteService.modifySite(siteForm);
        return ResponseResult.success(Code.REQUEST_SUCCESS);
    }

    @ApiOperation(value = "获取一个电站信息")
    @GetMapping("/getOne")
    public ResponseResult getOne(@ApiParam(value = "电站主键id", required = true)
                                 @RequestParam("siteId") String siteId) {
        SiteVo siteVo = siteService.getOneSiteById(siteId);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("site", siteVo);
    }

    @ApiOperation(value = "获取所有的电站信息")
    @GetMapping("/getAll")
    public ResponseResult getAll() {
        List<SiteVo> allSite = siteService.getAllSite();
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data(allSite);
    }

    @ApiOperation(value = "获取用户所在省所有的电站信息")
    @GetMapping("/getProvinceSites")
    public ResponseResult getThisProvinceAllSite(@ApiParam(value = "用户所在市", required = true)
                                                 @RequestParam("city") String city) {
        List<SiteVo> allSite = siteService.getThisProvinceAllSite(city);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data(allSite);
    }

    @ApiOperation(value = "分页查询附近电站信息")
    @PostMapping("/page")
    public ResponseResult pageQuery(@ApiParam(value = "分页条件对象")
                                    @RequestBody SiteQuery siteQuery) {
//                                    SiteQuery siteQuery) {
        System.out.println(siteQuery);
        Page<SiteVo> siteVoPage = siteService.pageQuery(siteQuery);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("sitePage", siteVoPage);
    }

    @ApiOperation(value = "获取电站个数")
    @GetMapping("/getCount")
    public ResponseResult siteCount() {
        QueryWrapper<Site> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("site_state", -1);
        int count = siteService.count(queryWrapper);
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("siteCount", count);
    }

    @ApiOperation(value = "获取所有待审批电站")
    @GetMapping("/getPending")
    public ResponseResult getPendingSite() {
        List<SiteVo> pendingSite = siteService.getPendingSite();
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("pendingSite", pendingSite);
    }


}



